package com.example.test_demo.stream;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: test
 * @description: t
 * @author: yanghua
 * @create: 2021-08-02 10:06
 **/
public class TestStream {
    public static void main(String[] args) {
        //listStream();
        //mapStream();
        long milliSecond = 1630555199500L;
        Date date = new Date();
        System.out.println(date.getTime());

        date.setTime(milliSecond);
        System.out.println(new SimpleDateFormat().format(date));
    }

    /**
     * 使用map遍历,但是我不接受list返回值,而是自己返回自己想要的值
     */
    public static void mapStream(){
        List<Student> students = new ArrayList<>();
        students.add(new Student(18,1,"小明"));
        students.add(new Student(19,2,"小红"));
        students.add(new Student(20,3,"小丽"));
        students.add(new Student(21,4,"小李"));
        //返回的是一个新的list
        List<Integer> list = students.stream().map(student -> student.getAge() + 10).collect(Collectors.toList());
        students.stream().forEach(student->{
            System.out.println(student.getAge());
        });
        Double collect = students.stream().map(student -> {
            return student.getAge() + 10;
        }).collect(Collectors.averagingInt(Integer::intValue));
        System.out.println(collect);

    }

    /**
     * 对比stream的map(有返回值可形成新的list,是在副本list进行的操作)和forEach(无返回值,是在原list进行的操作)
     */
    public static void listStream(){
        List<Student> students = new ArrayList<>();
        students.add(new Student(18,1,"小明"));
        students.add(new Student(19,2,"小红"));
        students.add(new Student(20,3,"小丽"));
        students.add(new Student(21,4,"小李"));
        List<Integer> list = students.stream().map(student -> student.getAge() + 10).collect(Collectors.toList());
        students.stream().forEach(student->{
            System.out.println(student.getAge());
        });
        list.stream().forEach(System.out::println);
        //
        students.stream().forEach(student->{
            student.setAge(student.getAge() + 10);
        });
        students.stream().forEach(student->{
            System.out.println(student.getAge());
        });

    }

}
